package game.assets;

import haxe.io.Path;
import game.data.WorldObjectData;
import game.worlds.GameWorld;
import haxe.Json;
import hx.assets.StringFuture;
import hx.assets.StyleFuture.StyleAssets;
import hx.assets.Future;

/**
 * 加载世界地图资源
 */
class WorldMapDataFuture extends Future<StyleAssets, String> {
	public var world:GameWorld;

	public function new(path:String) {
		super(path, false);
	}

	override function post() {
		new StringFuture("assets/worlds/world_objects.json").onComplete((text) -> {
			world.worldObjectData = new WorldObjectData(Json.parse(text));
			new StringFuture(getLoadData()).onComplete(map -> {
				var mapData = Json.parse(map);
				var files:Array<{
					id:String
				}> = mapData.files;
				var assets = new StyleAssets();
				assets.objects.set("map", mapData);
				for (file in files) {
					var config = world.worldObjectData.getConfig(file.id);
					switch config.type {
						case "png":
							assets.loadBitmapData(Path.join(["assets", config.path]));
						default:
							throw "unknown type" + config.type;
					}
				}
				assets.onComplete(a -> {
					this.completeValue(assets);
				}).onError(errorValue);
				assets.start();
			}).onError(this.errorValue);
		}).onError(this.errorValue);
	}
}
